﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Runtime.Serialization;
using System.Security.Permissions;

namespace Signage01
{
    [Serializable]
    public class ScreenTemplate : INotifyPropertyChanged
    {
        private String _name;
        private Screen _screenObject;
        private List<ScreenDivision> _listScreenDivision;
        private String _imageFilePath;
        private int _lastScreenDivIndex = 0;

        public int LastScreenDivIndex
        {
            get { return _lastScreenDivIndex; }
            set { _lastScreenDivIndex = value; }
        }

        public ScreenTemplate(Screen screenObject)
        {
            this._name = "ScreenTemplate";
            this._screenObject = screenObject;
            this._listScreenDivision = new List<ScreenDivision>();
            this._imageFilePath = "Resources/empty.png";
        }

        public string Name
        {
            get
            {
                return this._name;
            }
            set
            {
                this._name = value;
                NotifyPropertyChanged("Name");
            }
        }

        public Screen ScreenObject
        {
            get
            {
                return this._screenObject;
            }
            set
            {
                this._screenObject = value;
            }
        }

        public List<ScreenDivision> ListScreenDivision
        {
            get
            {
                return this._listScreenDivision;
            }
            set
            {
                this._listScreenDivision = value;
            }
        }

        public ScreenTemplate ShallowCopy()
        {
            return (ScreenTemplate)this.MemberwiseClone();
        }

        /*
        public static List<ScreenTemplate> getScreenTemplateObjectFromResource()
        {
            string serializationFile = "resources/screen/screenTemplate.bin";
            List<ScreenTemplate> listScreenTemplate;

            // retrieving the list from the file
            try
            {
                using (Stream stream = File.Open(serializationFile, FileMode.Open))
                {
                    var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

                    // if this line throws exception, the file is empty, so we have to create a new list in catch block
                    listScreenTemplate = (List<ScreenTemplate>)bformatter.Deserialize(stream);
                    return listScreenTemplate;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("The file is empty");
                listScreenTemplate = new List<ScreenTemplate>();
                return listScreenTemplate;
            }
        }
        */
        /*
        public static List<ScreenTemplate> getScreenTemplateObjectFromResource(Screen screenToFilter)
        {
            
            string serializationFile = "resources/screen/screenTemplate.bin";
            List<ScreenTemplate> listScreenTemplate;
            List<ScreenTemplate> filteredListScreenTemplate = new List<ScreenTemplate>();

            // retrieving the list from the file
            try
            {
                using (Stream stream = File.Open(serializationFile, FileMode.OpenOrCreate))
                {
                    var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

                    // if this line throws exception, the file is empty, so we have to create a new list in catch block
                    listScreenTemplate = (List<ScreenTemplate>)bformatter.Deserialize(stream);

                    foreach (ScreenTemplate st in listScreenTemplate)
                    {
                        if (st._screenObject.Equals(screenToFilter))
                        {
                            filteredListScreenTemplate.Add(st);
                        }
                    }
                    return filteredListScreenTemplate;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("The file is empty");
                listScreenTemplate = new List<ScreenTemplate>();
                return listScreenTemplate;
            }
        }
        */
         
        /*
        public static void saveScreenTemplateListObjectToResource(List<ScreenTemplate> listScreenTemplate)
        {
            List<ScreenTemplate> oldListScreenTemplate = getScreenTemplateObjectFromResource();

            oldListScreenTemplate.AddRange(listScreenTemplate);

            string serializationFile = "resources/screen/screenTemplate.bin";
            
            // saving the list to the file
            using (Stream stream = File.Open(serializationFile, FileMode.OpenOrCreate))
            {
                try
                {
                    var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                    bformatter.Serialize(stream, oldListScreenTemplate);
                    stream.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
        }
        */

        /*
        [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            if (info == null)
                throw new ArgumentNullException("info");

            //info.AddValue("Text", _Text);
        }

        */

        /*
        public static void addScreenTemplateObjectToResource(ScreenTemplate newScreenTemplate)
        {
            string serializationFile = "resources/screen/screenTemplate.bin";
            List<ScreenTemplate> oldListScreenTemplate = getScreenTemplateObjectFromResource();
            oldListScreenTemplate.Add(newScreenTemplate);
            // saving the list to the file
            using (Stream stream = File.Open(serializationFile, FileMode.OpenOrCreate))
            {
                var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                bformatter.Serialize(stream, oldListScreenTemplate);
            }
        }

        */
        
        public List<object> Items
        {
            get
            {
                List<object> childNodes = new List<object>();
                foreach (var sd in this._listScreenDivision)
                    childNodes.Add(sd);
                return childNodes;
            }
        }
        

        // implement interfact inotifyproperty
        
        private bool isSelected;
        public bool IsSelected
        {
            get { return this.isSelected; }
            set
            {
                if (value != this.isSelected)
                {
                    this.isSelected = value;
                    NotifyPropertyChanged("IsSelected");
                  
                }
            }
        }

       
        private bool isExpanded;
        public bool IsExpanded
        {
            get { return this.isExpanded; }
            set
            {
                if (value != this.isExpanded)
                {
                    this.isExpanded = value;
                    NotifyPropertyChanged("IsExpanded");
                }
            }
        }

        public String ImageFilePath{
            get
            {
                return this._imageFilePath;
            }
            set
            {
                this._imageFilePath = value;
                NotifyPropertyChanged("ImageFilePath");
            }
        }

		[field: NonSerialized]       
        public event PropertyChangedEventHandler PropertyChanged;

       
        public void NotifyPropertyChanged(string propName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
        
    }
}
